package com.netease.qa.biz.service.impl;

import com.nestease.qa.dao.api.UserDAO;
import com.nestease.qa.dao.entity.User;
import com.netease.qa.biz.dto.UserDTO;
import com.netease.qa.biz.enums.UserStatus;
import com.netease.qa.biz.service.UserService;
import com.netease.qa.biz.utils.CodeMachine;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author zoudongdong
 * @date 2018/2/20
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserDAO userDAO;

    public UserDTO verifyLogin(UserDTO userDTO) {
        User user = new User();
        user.setUserName(userDTO.getUserName());
        user.setPwd(userDTO.getPwd());

        User user2 = userDAO.verifyLogin(user);
        if (user2 == null) {
            return null;
        } else {
            userDTO.setUserId(user2.getUserId());
            return userDTO;
        }
    }

    public void userRegister(UserDTO userDTO) {
        User user = new User();
        user.setUserId(CodeMachine.genUserId());
        user.setUserName(userDTO.getUserName());
        user.setPwd(userDTO.getPwd());
        user.setStatus(UserStatus.Valid.getValue());

        userDAO.userRegister(user);
    }

    public boolean verifyUserName(String userName) {
        int count = userDAO.verifyUserName(userName);
        return count >= 1?false:true;
    }
}
